<?php
class CommonHelper {
	public static function gen_uuid(){
		$mongo_id = new MongoId();
		return $mongo_id->__toString();
	}
	
	/**
	 * 创建目录
	 * @param string $absolute_path
	 * @param int $mode
	 */
	public static function ymkdirs($absolute_path){
		if(!file_exists($absolute_path)){
			CommonHelper::ymkdirs(dirname($absolute_path));
			return mkdir($absolute_path, 0777 , true);
		}
		return true;
	}
	
	/**
	 * 根据提供的路径跟图标规格，生成规格图片路径
	 * @param string $path 原始图片路径
	 * @param string $thumb 规格，“宽x高”表示，比如：100x100
	 */
	public static function get_thumb($path, $thumb){
		$path = preg_replace('%(.*)/([^/]+.(jpg|jpeg|gif|png))%i', '$1/' . $thumb . '/$2', $path);
		return $path;
	}
	
	private static function get_mailer($is_html = true){
		require_once ROOT_PATH . '/protected/extensions/email/class.phpmailer.php';
		$mail = new PHPMailer();
		$mail->IsSMTP();					// 启用SMTP
		$mail->Host 	= 'smtp.qq.com';			//SMTP服务器
		$mail->SMTPAuth = true;					//开启SMTP认证
		$mail->Username = 'server@lingou.com';				// SMTP用户名
		$mail->Password = 'ri0514gd';				// SMTP密码
		$mail->From 	= 'server@lingou.com';			//发件人地址
		$mail->FromName = "server@lingou.com";				//发件人
		$mail->CharSet 	= 'utf-8';                // 这里指定字符集！
		$mail->Encoding = 'base64';
		$mail->IsHTML($is_html);					// 是否HTML格式邮件
		return $mail;
	}

	public static function mail_to($to, $subject, $message, $cc=null) {
		$mail = self::get_mailer();
		$mail->AddAddress($to);	//添加收件人
		if($cc !== null){
			$mail->AddCC($cc);
		}
		//$mail->AddReplyTo("info@example.com", "Information");	//回复地址
		$mail->WordWrap = 50;					//设置每行字符长度
		/** 附件设置
		$mail->AddAttachment("/var/tmp/file.tar.gz");		// 添加附件
		$mail->AddAttachment("/tmp/image.jpg", "new.jpg");	// 添加附件,并指定名称
		*/
	
		$mail->Subject = $subject;			//邮件主题
		$mail->Body    = $message;		//邮件内容
		$mail->AltBody = $message;	//邮件正文不支持HTML的备用显示
	
		if(!$mail->Send()){
			Yii::trace('发送邮件出错！' . $mail->ErrorInfo);
// 		echo "Message could not be sent. <p>";
// 		   echo "Mailer Error: " . $mail->ErrorInfo;
// 			   exit;
		}
	}
	
	public static function get_picture_path($path){
		return preg_replace('/\/\d+x\d+\/$/', '/', $path);
	}
	
	public static function get_picture_name($path){
		return preg_replace('/.*\/([^\/]+)$/', '$1', $path);
	}

	public static function _my_sqlite_escape_string($my_str){
		if(preg_match('/[\'|\"|`]/', $my_str)){
			//特殊字符添加反斜杠
			$my_str = preg_replace('/`/', "'", $my_str);
			$my_str = sqlite_escape_string($my_str);
		}
		return $my_str;
	}
	
	public static function start_with($haystack, $needle){
		return $needle === '' || strpos($haystack, $needle) === 0;
	}
	
	public static function end_with($str, $keyword){
		return $keyword === '' || substr($str, -strlen($keyword)) === $keyword;
	}


	/**
	 *
	 * @param unknown $data
	 */
	public static function fix($value_type, &$data){
		foreach ($data as $key => $value){
			if(!isset($value_type[$key])){
				continue;
			}
			$type = $value_type[$key];
			if($type == 'int'){
				if(is_array($value)){
					foreach ($value as $k => $v){
						$data[$key][$k] = intval($v);
					}
				}else if(is_string($value)){
					$data[$key] = intval($value);
				}
			}else if($type == 'float'){
				if(is_array($value)){
					foreach ($value as $k => $v){
						$data[$key][$k] = floatval($v);
					}
				}else if(is_string($value)){
					$data[$key] = floatval($value);
				}
			}
		}
	}
}

?>